$.namespace('act.processList');

act.processList = function() {

	return {
		init : function() {
			this.initGrid();
			this.initComponents();
		},
		initComponents : function(){
			$('#category').combobox({
				editable : false,
				data : [{text : '类别一',value : '1'},{text : '类别二',value : '2'},{text : '类别三',value : '3'}],
				valueFiled : 'value',
				textField : 'text'
			});
			
			$('#fm').ajaxForm({
				url: ctxRest + '/act/process/deploy',
				type: 'POST', 
				dataType: 'json',
				resetForm: true,
				beforeSend : function(){
					progressLoad();
				},
				success: function (data) {
					progressClose();
		            if (data && data.errcode == 0) {
		            	$('#deploydlg').dialog('close');
		            	$('#grid').datagrid('reload');
		            }
		        }
			});
		},
		initGrid : function() {
			$('#grid').datagrid({
				method : 'GET',
				url : ctxRest + '/act/process/list',
				contentType : 'application/json;charset=utf-8',
				fit : true,
				fitColumns : true,
				rownumbers : true,
				nowrap : true,
				striped : true,
				remoteSort : false,
				singleSelect : true,
				border : false,
				idField : 'id',
				columns : [ [ {
					title : '流程分类',
					field : 'process.category',
					width : 60,
					formatter : function(value, row, index){
						return row.process.category;
					}
				}, {
					title : '流程ID',
					field : 'process.id',
					width : 80,
					formatter : function(value, row, index){
						return row.process.id;
					}
				}, {
					title : '流程标识',
					field : 'process.key',
					width : 60,
					formatter : function(value, row, index){
						return row.process.key;
					}
				}, {
					title : '流程名称',
					field : 'process.name',
					width : 100,
					formatter : function(value, row, index){
						return row.process.name;
					}
				}, {
					title : '流程版本',
					field : 'process.version',
					width : 60,
					formatter : function(value, row, index){
						return row.process.version;
					}
				}, {
					title : '流程XML',
					field : 'process.resourceName',
					width : 80,
					formatter : function(value, row, index){
						return '<a href="' + ctxRest + '/act/process/resource/read?procDefId=' + row.process.id + '&resType=xml" target="_blank">' + row.process.resourceName + '</a>';
					}
				}, {
					title : '流程图',
					field : 'diagramResourceName',
					width : 80,
					formatter : function(value, row, index){
						return '<a href="' + ctxRest + '/act/process/resource/read?procDefId=' + row.process.id + '&resType=image" target="_blank">' + row.process.diagramResourceName + '</a>';
					}
				}, {
					title : '部署时间',
					field : 'deployment.deploymentTime',
					width : 85,
					formatter : function(value, row, index){
						return moment(row.deployment.deploymentTime).format('YYYY-MM-DD HH:mm:ss');
					}
				}, {
					title : '操作',
					field : 'option',
					width : 120,
					formatter : function(value, row, index){
						var opts = '';
//						if (row.suspended) {
//							opts += '<a href="javascript:void(0)" onclick="${ctx}/act/process/update/active?procDefId=${process.id}">[ 激活 ]</a>';
//						} else {
//							opts += '<a href="javascript:void(0)" onclick="${ctx}/act/process/update/suspend?procDefId=${process.id}">[ 挂起 ]</a>';
//						}
						opts += '<a href="javascript:void(0);" onclick="act.processList.remove(' + "'" + row.deployment.id + "'" + ')">[ 删除 ]</a>';
//						opts += '<a href="javascript:void(0)" onclick="${ctx}/act/process/convert/toModel?procDefId=${process.id}">[ 转换为模型 ]</a>';
						return opts;
					}
				} ] ],
				toolbar : '#toolbar'
			});
		},
		showDeployForm : function() {
			$('#deploydlg').dialog('open');
		},
		deploy : function(){
			$('#fm').submit();
		},
		remove : function(obj) {
			var row = $('#grid').datagrid('getSelected');
			if (!row && !obj) {
				$.messager.alert('提示', '请选择要删除的流程', 'info');
				return;
			}
			if (!obj) {
				obj = row.deployment.id;
			}
			
			$.messager.confirm('提示', '确定要删除？', function(flag){
				if (flag) {
					$.ajax({
						type : 'POST',
						url : ctxRest + '/act/process/delete',
						contentType : 'application/json;charset=utf-8',
						data : obj,
						success : function(result){
							if (result && result.errcode == 0) {
				            	$('#grid').datagrid('reload');
				            }
						}
					});
				}
			})
		}
		
	};
}();

$(function() {
	act.processList.init();
})